package ticket

import (
	"context"
	"go.uber.org/zap"
	"ticket_srv/config"
	"ticket_srv/internal/model/dao"
	__ "ticket_srv/proto"
)

// 实现方法
func (s *TicketService) BuyTicket(ctx context.Context, req *__.BuyTicketRequest) (resp *__.BuyTicketResponse, err error) {
	var ticket dao.Ticket
	config.DB.Where("ticket_name=?", req.TicketName).Limit(1).Find(&ticket)
	if ticket.ID == 0 {
		zap.S().Error("没有该票")
		return
	}
	if ticket.TicketNum < int(req.TicketNum) {
		zap.S().Error("余票不足")
		return
	}
	num := ticket.TicketNum - int(req.TicketNum)
	config.DB.Where("ticket_name=?", req.TicketName).Update("ticket_num", num).Find(&ticket)
	return &__.BuyTicketResponse{
		Id: int64(ticket.ID),
	}, nil
}
